package com.ls.lserp.modules.produce.service;





import java.math.BigDecimal;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.dao.MaterialInboundDao;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.order.entity.OrderMateriel;
import com.ls.lserp.modules.produce.dao.ProduceLicenseDao;
import com.ls.lserp.modules.produce.dao.ProduceNoticeDao;
import com.ls.lserp.modules.produce.entity.ProduceLicense;
import com.ls.lserp.modules.produce.entity.ProduceNotice;
import com.ls.lserp.modules.produce.entity.ProducePlan;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
@Service
@Transactional(readOnly = false)
public class ProduceNoticeService extends CrudService<ProduceNoticeDao,ProduceNotice>{

	
	
	@Autowired
    private ProduceNoticeDao produceNoticeDao;
	
	@Autowired
    private ProduceLicenseDao produceLicenseDao;
	
	@Autowired
	private MaterialInboundDao materialInboundDao ;
	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public Page<ProduceNotice> findProduceNoticeList(Page<ProduceNotice> page, ProduceNotice produceNotice) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
	        // 设置分页参数
	    	produceNotice.setPage(page);
	        // 执行分页查询
	        page.setList(produceNoticeDao.findList(produceNotice));
	        return page;
	    }
	    /**
		 * 查询班组
		 * 
		 * @param user
		 * @return
		 */
	    public List<ProduceNotice> findGroupList(){
	    	
	    	return produceNoticeDao.findGroupList();
	    }
	    
		/**
		 * 查询一条生产通知单信息
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceNotice getNotice(ProduceNotice produceNotice) {
	    	produceNotice = produceNoticeDao.get(produceNotice);
	    	ProducePlan producePlan = produceNoticeDao.getPlan(produceNotice.getPlanID());
	    	OrderMateriel orderMateriel = new OrderMateriel();
	    	if ("1".equals(produceNotice.getNoticeFlag())) {
	    		orderMateriel = produceNoticeDao.getContractInfo(produceNotice.getContractID());
			}else {
				orderMateriel = produceNoticeDao.getOrderInfo(produceNotice.getIndentID());
			}
	    	produceNotice.setProducePlan(producePlan);
	    	produceNotice.setOrderMateriel(orderMateriel);
	        return produceNotice;
	        
	    }
	    
	    /**
		 * 新增一条生产制单
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceLicense createNewLicense(ProduceNotice produceNotice)throws Exception {
	    	//当前日期
	    	Date newDate = new Date();
	    	produceNotice = getNotice(produceNotice);
	    	ProduceLicense produceLicense = new ProduceLicense();
	    	if ("1".equals(produceNotice.getNoticeFlag())) {
				produceLicense.setContractID(produceNotice.getContractID());
				produceLicense.setOgnizationID(produceNotice.getOgnizationID());
				produceLicense.setIndentNO(produceNotice.getOrderMateriel().getContractNo());
				produceLicense.setProFlag(1);
				produceLicense.setShippmentDate(produceNotice.getRemarks());
			}else {
				OrderMateriel orderMateriel = produceNoticeDao.getOrderInfo(produceNotice.getIndentID());
				/*StringBuilder conCode = new StringBuilder();
				conCode.append("L");
				conCode.append(DateUtils.formatDate(newDate, "yyyyMMdd"));
				Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_produceLicense", 3);*/
				produceLicense.setContractID(orderMateriel.getContractId());
				produceLicense.setOgnizationID(Long.parseLong(orderMateriel.getOgnizationId()));
				produceLicense.setIndentID(orderMateriel.getIndentId());
				produceLicense.setStyleID(orderMateriel.getStyleId());
			}
	    	produceLicense.setProductionNO(produceNotice.getNoticeNO().replace("N", "L"));
	    	produceLicense.preInsert();
	    	produceLicense.setProEfficiency(produceNotice.getNoticeEfficiency());
	    	produceLicense.setProNumber(produceNotice.getNoticeNumber());
	    	produceLicense.setProPeriod(produceNotice.getNoticePeriod());
	    	//天数计算：(结束日期-开始日期)/(24*60*60*1000)+1
	    	produceLicense.setProBeginDate(produceNotice.getNoticeBeginDate());
	    	produceLicense.setProEndDate(produceNotice.getNoticeEndDate());
	    	produceLicense.setProDeliveryDate(produceNotice.getNoticeDeliveryDate());
	    	produceLicense.setProQuantity(produceNotice.getNoticeQuantity());
	    	produceLicense.setVersion(1);
	    	produceLicense.setProStatus("1");
	    	produceLicense.setMaterialStatus("1");
	    	produceLicense.setUserGroupID(produceNotice.getUserGroupID());
	    	//来源为通知单
	    	produceLicense.setSourceNotice("true");
	    	return produceLicense;
	    	/*produceLicenseDao.insert(produceLicense);
	    	//插入通知单制单关联表
	    	produceNotice.preInsert();
	    	produceNotice.setProductionId(produceLicense.getId());
	    	produceNoticeDao.insertNoticeLicense(produceNotice);
	    	//更新通知单状态
	    	produceNotice.setMaterialStatus(2);
	    	produceNoticeDao.updateStatus(produceNotice);*/
	    }
	    /**
		 * ID查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceNotice findByID(Long id){
	    	ProduceNotice produceNotice = produceNoticeDao.get(id);
	    	ProducePlan producePlan = produceNoticeDao.getPlan(produceNotice.getPlanID());
	    	OrderMateriel orderMateriel = new OrderMateriel();
	    	if ("1".equals(produceNotice.getNoticeFlag())) {
	    		orderMateriel = produceNoticeDao.getContractInfo(produceNotice.getContractID());
			}else {
				orderMateriel = produceNoticeDao.getOrderInfo(produceNotice.getIndentID());
			}
	    	produceNotice.setProducePlan(producePlan);
	    	produceNotice.setOrderMateriel(orderMateriel);
	    	return produceNotice;
	    }
	    /**
		 * 获取合同订单信息
		 * 
		 * @param user
		 * @return
		 */
	    public OrderMateriel getOrderInfo(Long id){
	    	
	    	return produceNoticeDao.getOrderInfo(id);
	    }
	    
	    /**
		 * @Title: 合同订单选择一览
		 * @Description: 一览查询
		 * @param page
		 * @param orderMateriel
		 * @return Page<OrderMateriel>
		 */
		public Page<OrderMateriel> findIndentList(Page<OrderMateriel> page, OrderMateriel orderMateriel) {
			
			// 设置分页参数
			orderMateriel.setPage(page);
			orderMateriel.setAuditPhaseCd(ResourcesUtil.getConfig("reserver_meterial_ok"));
			// 执行分页查询
			page.setList(produceNoticeDao.findIndentList(orderMateriel));
			return page;
		}
	    
	    /**
		 * 新增/修改一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void saveProduceNotice(ProduceNotice produceNotice) {
	    	// 判断新建编辑
	    		//若为null 执行插入语句（新建）
	    		if (produceNotice.getId()==null){
	    			// 编辑
	    			StringBuilder conCode = new StringBuilder();
    				conCode.append("N");
    				conCode.append(DateUtils.formatDate(new Date(), "yyyyMMdd"));
    		        Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_produceNotice", 3);
    		        //查找该款式订单是否有核料采购，okCount为0则没有，如果没有，通知单物料状态设置为备料完成
    		        long okCount = materialInboundDao.getCount(produceNotice.getIndentID());
    		        if (okCount == 0) {
    		        	produceNotice.setMaterialStatus(1);
					}
    		        produceNotice.setNoticeNO(sequence.getReturnCode());
    		        produceNotice.setNoticeFlag("0");
    		        produceNotice.preInsert();
    		        if (ResourcesUtil.getConfig("notice_ns00").equals(produceNotice.getContractIndentEntity().getStockStatus())) {
    		        	produceNotice.setMaterialStatus(0);
					}else {
						produceNotice.setMaterialStatus(1);
					}
    		        produceNoticeDao.insert(produceNotice);
    		        produceNoticeDao.insertPlanNotice(produceNotice);
	    				
	    		}else{
	    			// 更新数据
	    			produceNotice.preInsert();
	    			produceNoticeDao.update(produceNotice);
	    		}			
	    }
	    
	    /**
		 * 提交一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void updateStatus(ProduceNotice produceNotice) {
	    			// 更新数据
	    	produceNotice.preInsert();
	    	produceNotice.setMaterialStatus(1);
	    	produceNoticeDao.updateStatus(produceNotice);
	    }
	    /**
		 * 删除一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void deleteProducePlan(ProduceNotice produceNotice) {
	    	// 更新数据
	    		produceNotice.preInsert();
	    		produceNotice.setDeleteFlag("1");
	    		produceNoticeDao.delete(produceNotice);
	    }
	    
	    /**
	     * 获取备料详情
	     * @param produceNotice
	     */
	    public List<OrderMateriel> findStockDetails(ProduceNotice produceNotice){
	    	return produceNoticeDao.findStockDetails(produceNotice);
	    }
}
